<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>

<!DOCTYPE html>
<html lang="UTF-8">
<%@include file="/WEB-INF/include-head.jsp" %>

<body>

<%@include file="/WEB-INF/include-nav.jsp" %>
<link rel="stylesheet" href="css/pagination.css"/>
<script type="text/javascript" src="jquery/jquery.pagination.js"></script>
<script type="text/javascript" src="layer/layer.js" charset="utf-8"></script>
<script type="text/javascript" src="crowd/my-role.js" ></script>
<script type="text/javascript">
    $(function(){
        //分页初始化数据
        window.pageNum = 1 ;
        window.pageSize = 5 ;
        window.keyword = "" ;
        //调用执行分页的函数
        generatePage();
        // 给查询按钮绑定单击响应函数
        $("#searchBtn").click(function () {
            //获取关键词
            window.keyword = $("#keywordInput").val() ;
            //调用执行分页的函数
            generatePage();
        });
        //点击新增打开模态窗
        $("#showAddModelBtn").click(function () {
            $("#addModal").modal("show");
        });
        $("#saveRoleBtn").click(function () {
            //获取输入的角色名
            // addModal表示找到整个模态窗
            // 空格表示在后代元素中继续找
            // [name=roleName]表示匹配name属性等于roleName的元素
            var roleName = $.trim($("#addModal [name=roleName]").val());


            // 发送ajax
            //保存角色不能重复
            $.ajax({
                "url":"role/save.json",
                "type":"post",
                "data":{
                    "name":roleName
                },
                "dataType":"json",
                "success":function (response) {
                    var result = response.result ;
                    if(result == "SUCCESS"){
                        layer.msg("操作成功！");

                        window.pageNum = 99999 ;
                        generatePage() ;
                    }
                    if(result == "FAILED"){
                        layer.msg("操作失败！"+response.message);
                    }
                },
                "error":function (response) {
                    layer.msg(response.status+" "+response.statusText);
                }
            });
            // 关闭
            $("#addModal").modal("hide");

            $("#addModal [name=roleName]").val("");

        });


        $("#rolePageBody").on("click",".pencilBtn",function () {
            $("#editModal").modal("show") ;
            var roleName = $(this).parent().prev().text();
            // 获取当前角色的id
            var id = $(this).parent().name ;
            console.log(this.id);
            window.roleId = this.id ;
            // 使用roleName的值设置模态窗中的文本框
            $("#editModal [name=roleName]").val(roleName) ;
        });

        // 给更新模态框中的更新按钮绑定单击响应函数
        $("#updateRoleBtn").click(function () {
            var roleName = $("#editModal [name=roleName]").val() ;
            $.ajax({
               "url":"role/update.json",
                "type":"post",
               "data":{
                   "id":window.roleId,
                   "name":roleName
               },
                "dataType":"json",
                "success":function (response) {
                    var result = response.result ;
                    if(result == "SUCCESS"){
                        layer.msg("操作成功！")
                        generatePage() ;
                    }
                    if(result == "FAILED"){
                        layer.msg("操作失败！"+response.message);
                    }
                },
                "error":function (response) {
                    layer.msg(response.status+" "+response.statusText);
                }
            });
            // 关闭
            $("#editModal").modal("hide");
        });

        //测试代码
        // var roleArray = [{roleId:5,roleName:"aaa"},{roleId:5,roleName:"bbb"},{roleId:5,roleName:"ccc"}]
        // showConfirmModal(roleArray) ;

        // 点击确认模态窗中的确认删除     @RequestMapping("/role/remove/by/role/id/array.json")

        $("#removeRoleBtn").click(function () {
            var requestBody = JSON.stringify(window.roleIdArray);
            $.ajax({
                "url":"role/remove/by/role/id/array.json",
                "type":"post",
                "data":requestBody,
                "contentType":"application/json;charset=UTF-8",
                "dataType":"json",
                "success":function (response) {
                    var result = response.result ;
                    if(result == "SUCCESS"){
                        layer.msg("操作成功！")
                        generatePage() ;
                    }
                    if(result == "FAILED"){
                        layer.msg("操作失败！"+response.message);
                    }
                },
                "error":function (response) {
                    layer.msg(response.status+" "+response.statusText);
                }
            });
            // 关闭
            $("#confirmModal").modal("hide");
            document.getElementById("summaryBox").checked = false ;
        })

        // 给单条数据绑定单击事件
        $("#rolePageBody").on("click",".removeBtn",function () {

            var roleName = $(this).parent().prev().text();
            var roleArray = [{
                roleId:this.id,
                roleName:roleName
            }];

            showConfirmModal(roleArray);
        });

        // 给总summaryBox绑定单击响应函数
        $("#summaryBox").click(function () {
            // 获取当前多选框自身状态
            var currentStatus = this.checked;
            // 多选框的状态设置其他框
            $(".itemBox").prop("checked",currentStatus);
        });

        // 全选、全不选反向操作
        $("#rolePageBody").on("click",".itemBox",function () {
            var checkedBoxCount = $(".itemBox:checked").length;
            var totalBoxCount = $(".itemBox").length ;
            // 使用二者的比较设置checkBox
            $("#summaryBox").prop("checked",checkedBoxCount == totalBoxCount) ;
        });
        
        // 给批量删除的按钮绑定单击响应函数
        $("#batchRemoveBtn").click(function () {
            // 创建一个数组对象用来存放后面获取到的角色对象
            var roleArray = [];
            //遍历当前选中的多选框
            $(".itemBox:checked").each(function () {
                // 使用this引用当前遍历得到的多选框
                var roleId = this.id ;
                // 通过DOM操作获取角色名称
               var roleName =  $(this).parent().next().text();

               roleArray.push({
                   "roleId":roleId,
                   "roleName":roleName
               });
            });

            //检查roleArray的长度是否为0
            if(roleArray.length == 0){
                layer.msg("请至少选择一个执行删除")
                return ;
            }
            //调用专门的函数打开模态窗
            showConfirmModal(roleArray) ;
        });
    });
</script>
<div class="container-fluid">
    <div class="row">
        <%@include file="/WEB-INF/include-sidebar.jsp" %>
        <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
            <div class="panel panel-default">
                <div class="panel-heading">
                    <h3 class="panel-title"><i class="glyphicon glyphicon-th"></i> 数据列表</h3>
                </div>
                <div class="panel-body">
                    <form class="form-inline" role="form" style="float:left;">
                        <div class="form-group has-feedback">
                            <div class="input-group">
                                <div class="input-group-addon">查询条件</div>
                                <input id="keywordInput" class="form-control has-success" type="text" placeholder="请输入查询条件">
                            </div>
                        </div>
                        <button id="searchBtn" type="button" class="btn btn-warning"><i class="glyphicon glyphicon-search"></i> 查询</button>
                    </form>
                    <button id="batchRemoveBtn" type="button" class="btn btn-danger" style="float:right;margin-left:10px;"><i class=" glyphicon glyphicon-remove"></i> 删除</button>
                    <button id="showAddModelBtn" type="button" class="btn btn-primary" style="float:right;" ><i class="glyphicon glyphicon-plus"></i> 新增</button>
                    <br>
                    <hr style="clear:both;">
                    <div class="table-responsive">
                        <table class="table  table-bordered">
                            <thead>
                            <tr>
                                <th width="30">#</th>
                                <th width="30"><input id="summaryBox" type="checkbox"></th>
                                <th>名称</th>
                                <th width="100" style="text-align: center">操作</th>
                            </tr>
                            </thead>
                            <tbody id="rolePageBody">
                            </tbody>
                            <tfoot>
                            <tr>
                                <td colspan="6" align="center">
                                    <div id="Pagination" class="pagination"><!-- 这里显示分页 --></div>
                                </td>
                            </tr>

                            </tfoot>
                        </table>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
<%@include file="/WEB-INF/modal-role-add.jsp"%>
<%@include file="/WEB-INF/modal-role-edit.jsp"%>
<%@include file="/WEB-INF/modal-role-comfirm.jsp"%>
</body>
</html>